package com.chinaisafe.user.controller;

import com.chinaisafe.base.common.validated.UpdateGroup;
import com.chinaisafe.module.user.request.SysRoleReqVO;
import com.chinaisafe.module.user.response.SysRoleRespVO;
import com.chinaisafe.user.biz.SysRoleBiz;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysRoleService;
import org.springframework.web.bind.annotation.RestController;
import com.chinaisafe.base.common.validated.AddGroup;

/**
 * SysRoleController
 * Description：角色接口层
 *
 * @author tby
 * @date  2024-07-11 17:32:41
 */
@RestController
@RequestMapping("/sysRole")
@Tag(name = "角色接口层", description = "角色接口层")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Resource
    private SysRoleBiz sysRoleBiz;

    @GetMapping("/pageList")
    @Operation(summary = "获取角色分页列表", description = "获取角色分页列表")
    public Answer<IPage<SysRoleRespVO>> pageList(SysRoleReqVO sysRoleReqVO) {
        IPage<SysRoleRespVO> pageList = sysRoleService.getPageList(sysRoleReqVO);
        return Answer.success("获取成功", pageList);
    }

    @PostMapping()
    @Operation(summary = "新增角色", description = "新增角色")
    @OperationLog(title = "角色-新增", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysRoleRespVO> add(@RequestBody @Validated(AddGroup.class) SysRoleReqVO sysRoleReqVO) {
        SysRoleRespVO sysRoleRespVO = sysRoleService.add(sysRoleReqVO);
        return Answer.success("保存成功", sysRoleRespVO);
    }

    @PutMapping()
    @Operation(summary = "根据id编辑角色", description = "根据id编辑角色")
    @OperationLog(title = "角色-根据id编辑", businessType = BusinessTypeEnum.UPDATE)
    public Answer<SysRoleRespVO> update(@RequestBody @Validated(UpdateGroup.class) SysRoleReqVO sysRoleReqVO) {
        SysRoleRespVO sysRoleRespVO = sysRoleService.update(sysRoleReqVO);
        return Answer.success("修改成功", sysRoleRespVO);
    }

    @GetMapping("/{sysRoleId}")
    @Operation(summary = "根据id获取角色详情", description = "可以获取到角色包含的用户")
    public Answer<SysRoleRespVO> getDetailById(@PathVariable Long sysRoleId) {
        SysRoleRespVO sysRoleRespVO = sysRoleBiz.getDetailById(sysRoleId);
        return Answer.success("获取成功", sysRoleRespVO);
    }

    @DeleteMapping()
    @Operation(summary = "根据id删除角色", description = "根据id删除角色")
    @OperationLog(title = "角色-根据id删除", businessType = BusinessTypeEnum.DELETE)
    public Answer<Void> deleteByIds(@RequestParam("ids") Long[] ids) {
        sysRoleService.deleteByIds(ids);
        return Answer.success("删除成功");
    }

    @PostMapping("enableSwitch")
    @Operation(summary = "角色启用/禁用", description = "参数请放在?后面")
    public Answer<Void> enableSwitch(Long id) {
        sysRoleBiz.enableSwitch(id);
        return Answer.success("操作成功");
    }
}

